﻿using System.Windows.Forms;
using DbManager;
using System.Data;
using DbManager.DataManager;
using DbManager.QueryBuilder;

namespace TAProject.UI.Service.Price
{
    public partial class FormSelectItem : FormTemplate
    {
        private readonly Form _parentForm;
        private readonly IDataManager _dataManager;
        private readonly IConnectionManager _connetctionManager;
        private readonly IQuery _iQuery;

        public int? Id { private set; get; }

        private readonly int? _filterValue;
        private readonly string _filterColumn;

        public FormSelectItem(Form parentForm, IQuery iQuery, IDataManager dataManager,
                              IConnectionManager connetctionManager, string title)
            : base(parentForm)
        {
            InitializeComponent();

            _parentForm = parentForm;
            _dataManager = dataManager;
            _connetctionManager = connetctionManager;

            _iQuery = iQuery;
            labelTitle.Text = title;

            FillDataGrid();

            checkBoxShowAll.Enabled = false;
            _parentForm.Enabled = false;
        }

        public FormSelectItem(Form parentForm, IQuery iQuery, IDataManager dataManager,
                              IConnectionManager connetctionManager, string title, int filterValue, string filterColumn)
            : base(parentForm)
        {
            InitializeComponent();

            _parentForm = parentForm;
            _dataManager = dataManager;
            _connetctionManager = connetctionManager;

            _iQuery = iQuery;
            _filterValue = filterValue;
            _filterColumn = filterColumn;
            labelTitle.Text = title;

            FillDataGrid(_filterValue.Value, _filterColumn);

            _parentForm.Enabled = false;
        }

        private void FormClose()
        {
            _parentForm.Enabled = true;
            Close();
        }

        private void FillDataGrid()
        {
            var dataSource = _dataManager.GetDataTable(_iQuery.GetSelectQuery(),
                                                       _connetctionManager.GetConnection());
            if (dataSource != null)
            {
                dataGridView.DataSource = dataSource;
                dataGridView.Columns[0].Visible = false;
            }
            else
            {
                MessageBox.Show("Ошибка получения данных из БД.");
                FormClose();
            }
        }

        private void FillDataGrid(int filterValue, string filterColumn)
        {
            var dataSource = _dataManager.GetDataTable(_iQuery.GetSelectQuery(),
                                                       _connetctionManager.GetConnection());
            if (dataSource != null)
            {
                var filteredDataTable = dataSource.AsEnumerable().
                    Where(row => row.Field<int>(filterColumn) == filterValue).CopyToDataTable();
                if (filteredDataTable.Rows.Count > 0)
                {
                    dataGridView.DataSource = filteredDataTable;

                    foreach (DataGridViewColumn column in dataGridView.Columns)
                    {
                        if (column.Name.StartsWith("Hide"))
                            column.Visible = false;
                    }
                }
                else
                {
                    MessageBox.Show("Записи по данному запросу отсутствуют");
                    Id = null;
                    FormClose();
                }
            }
            else
            {
                MessageBox.Show("Ошибка получения данных из БД.");
                FormClose();
            }
        }

        private void ButtonSubmitClick(object sender, System.EventArgs e)
        {
            if (dataGridView.CurrentRow == null) return;
            Id = (int) dataGridView.Rows[dataGridView.CurrentRow.Index].Cells[0].Value;
            if (Id != 0)
                FormClose();
            else
                MessageBox.Show("Ошибка выбора элемента.");
        }

        private void ButtonCancelClick(object sender, System.EventArgs e)
        {
            Id = null;
            FormClose();
        }

        private void CheckBoxShowAllCheckedChanged(object sender, System.EventArgs e)
        {
            if (checkBoxShowAll.Checked)
            {
                FillDataGrid();
            }
            else
            {
                if(!string.IsNullOrEmpty(_filterColumn) && _filterValue != null)
                    FillDataGrid(_filterValue.Value, _filterColumn);
            }
        }
    }
}